package fr.pud.client.controller.address;

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.JOptionPane;

import fr.pud.client.i18n.LanguagesConstants;
import fr.pud.client.images.ImagesDatabase;
import fr.pud.client.model.Address;
import fr.pud.client.model.PUDStation;
import fr.pud.client.model.PUDStationDatabase;
import fr.pud.client.view.MainFrame;

/**
 * Apr 13, 2012 - Projet_Ose.
 * @author Simon Devineau, Adrien Hierry, Leopold Houdin, Pierre Reliquet
 *         Ecole des Mines de Nantes
 *         AddAddressToDatabase.java
 */
public class AddAddressToDatabase extends AbstractAction {
    /**
     * 
     */
    private static final long serialVersionUID = -4004300389694278625L;
    private Address           addressToAdd;

    /**
     * Add an address to the database after a search.
     */
    public AddAddressToDatabase(Address anAddress) {
        this.putValue(NAME, LanguagesConstants.LANGUAGE
                .getString(LanguagesConstants.ADD_ADDRESS));
        this.putValue(SHORT_DESCRIPTION, LanguagesConstants.LANGUAGE
                .getString(LanguagesConstants.ADD_ADDRESS));
        this.putValue(SMALL_ICON, ImagesDatabase.PLUS);
        this.addressToAdd = anAddress;
    }

    @Override
    public void actionPerformed(ActionEvent aArg0) {
        if (PUDStationDatabase.getInstance().getStations()
                .add(new PUDStation(this.addressToAdd))) {
            JOptionPane
                    .showMessageDialog(
                            MainFrame.getMainFrame(),
                            LanguagesConstants.LANGUAGE
                                    .getString(LanguagesConstants.SUCCESSFULL_OPERATION),
                            LanguagesConstants.LANGUAGE
                                    .getString(LanguagesConstants.ADD_ADDRESS),
                            JOptionPane.INFORMATION_MESSAGE,
                            ImagesDatabase.OSM_ICON);
        }
    }
}
